package com.codeprophecy.client;

import com.codeprophecy.client.doc.DocumentEntry;
import com.codeprophecy.client.doc.DocumentFeed;
import com.codeprophecy.client.doc.DocumentFeedCallback;
import com.codeprophecy.client.doc.DocumentsService;
import com.codeprophecy.client.doc.ServiceCallback;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.gadgets.client.NeedsLockedDomain;
import com.google.gwt.gadgets.client.UserPreferences;
import com.google.gwt.gdata.client.DateTime;
import com.google.gwt.gdata.client.Feed;
import com.google.gwt.gdata.client.GData;
import com.google.gwt.gdata.client.GDataSystemPackage;
import com.google.gwt.gdata.client.GoogleService;
import com.google.gwt.gdata.client.atom.Updated;
import com.google.gwt.gdata.client.contacts.ContactFeed;
import com.google.gwt.gdata.client.contacts.ContactFeedCallback;
import com.google.gwt.gdata.client.contacts.ContactsService;
import com.google.gwt.gdata.client.impl.CallErrorException;
import com.google.gwt.gdata.client.impl.Callback;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;

@Gadget.ModulePrefs(title = "Document List r9", author = "Code Prophecy", author_email = "codeprophecy1@gmail.com")
@Gadget.InjectModulePrefs(files = { "ext/lock.xml", "ext/oauth.xml" })
@Gadget.InjectContent(files = { "ext/script.xml","ext/gadget.xml" })
public class Gdocs extends Gadget<UserPreferences> {

	String applicationName = "Google Documents";
	String serviceName = "writely";
	String feedUrl = "http://docs.google.com/feeds/documents/private/full";
	RootPanel root ;
	FlexTable feed;

	@Override
	protected void init(UserPreferences preferences) {
		// TODO Auto-generated method stub
		
		root = RootPanel.get("main");
		TabPanel tabs = new TabPanel();

		feed = new FlexTable();
		feed.setText(0, 0, "Select");
		feed.setText(0, 1, "Title");
		feed.setText(0, 2, "Last Modified");
		
		tabs.add(feed, "Tab 1");
		root.add(tabs);
		
		DocumentsService.newInstance(new ServiceCallback() {
			
			@Override
			public void onSuccess(DocumentsService service) {
				// TODO Auto-generated method stub
				postOauth(service);
			}
			
			@Override
			public void onFailure(JavaScriptObject obj) {
				// TODO Auto-generated method stub
				Window.alert("failed to login");
			}
		});
	}
	
	private void showOnly(String page){
		String[] pages = {"approval","waiting","main","error","loading"};
		for(String pg : pages){
			Element e = DOM.getElementById(pg);
			if(e==null) continue;
			Style s = e.getStyle();
			if(pg==page){
				s.setDisplay(Display.NONE);
			}else{
				s.setDisplay(Display.BLOCK);
			}
		}
	}

	
	private void log(Object s){
		//k1l1manjar0
		Element el = DOM.getElementById("logger");
		String t = el.getInnerHTML();
		el.setInnerHTML(t+s.toString()+"\n");
	}

	private void postOauth(DocumentsService service){
		String uri = "http://docs.google.com/feeds/documents/private/full";
		service.getDocumentFeed(uri, new DocumentFeedCallback() {
			
			@Override
			public void onSuccess(DocumentFeed result) {
				// TODO Auto-generated method stub
				for(DocumentEntry e: result.getEntries()){
					DateTime ts = e.getUpdated().getValue();
					CheckBox chk = new CheckBox();
					String title = e.getTitle().getText();
				}
			}
			
			@Override
			public void onFailure(CallErrorException caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

}
